# Maintainer: Zhang Hai <dreaming.in.code.zh@gmail.com>
# Contributor: Alexander F Rødseth <xyproto@archlinux.org>
# Contributor: Daniel Micay <danielmicay@gmail.com>
# Contributor: Gordin <9ordin@gmail.com>

# Finding dependencies:
# ELF dependencies:
# cd /lib/ && find /opt/android-sdk/tools/ -type f -executable \
#   | LANG=C xargs readelf -d 2>/dev/null \
#   | grep -oP '(?<=Shared library: \[).*(?=\])' | LANG=C xargs pacman -Qo \
#   | cut -d' ' -f5 | sort | uniq
# .so in JAR files:
# find /opt/android-sdk/tools/ -type f -exec unzip -l {} 2>&1 \; \
#   | grep -P '^Archive:|\.so$' \
#   | awk 'BEGIN { archive = ""; } { if (NF == 2) { archive = $0; } else { \
#     if (length(archive) > 0) { print archive; archive="" } print $0; } }'
# Note that dependency on libxtst is from swt.

pkgname=android-sdk
pkgver=26.1.1
pkgrel=1
pkgdesc='Google Android SDK'
arch=('x86_64' 'aarch64')
groups=('blackarch' 'blackarch-mobile')
url='https://developer.android.com/studio/releases/sdk-tools.html'
license=('custom')
depends=('java-environment' 'libxtst' 'fontconfig' 'freetype2' 'lib32-gcc-libs'
         'lib32-glibc' 'libx11' 'libxext' 'libxrender' 'zlib')
optdepends=('android-emulator: emulator has become standalone since 25.3.0'
            'android-sdk-platform-tools: adb, aapt, aidl, dexdump and dx'
            'android-udev: udev rules for Android devices')
install="${pkgname}.install"
source=('https://dl.google.com/android/repository/sdk-tools-linux-4333796.zip'
        "${pkgname}.sh"
        "${pkgname}.csh"
        "${pkgname}.conf"
        'license.html')
sha512sums=('5050bf5cd8327c59f420f2072369d299e9193a3d697740b5b253199635299f90f34f4c4da2467f3af356c5c6226adfce9aa311ef2c9673a6ba664ab5c3173d18'
            '304aa74f655badab08de50c25eca62d19867dc43f810842d38f8b69d4e5e7981fe0e4ead4e56fd7b9f83ed03e649227b785dec0b979ed92e4f0ceb433d565c5c'
            '34205bc32b977e3621ab3e4861ee6807adda08859c34f43520243c47daab28a5497296d2352d8f821fdbe0dda28ed12db7b8978b476f97688dcd0e881a6ebd7d'
            'e23fa38549f801a79323b47a5f7406e6961060710c526f859e1b0f6454c1b09687a1f9bf4850b1a127dbeac7a052946daf009a255804e3fed420d993912ccd7c'
            '23f1533682c39e2ae2ae40083de343abcd6ed0a523eacdac58768067f8f450b1cec30073993dd7b972a5b8125136ed29a043ae894dff046fd75bb04c4d21a0f4')

package() {

  install -Dm755 "${pkgname}.sh" "${pkgdir}/etc/profile.d/${pkgname}.sh"
  install -Dm755 "${pkgname}.csh" "${pkgdir}/etc/profile.d/${pkgname}.csh"
  install -Dm644 "${pkgname}.conf" "${pkgdir}/etc/ld.so.conf.d/${pkgname}.conf"
  install -Dm644 license.html "${pkgdir}/usr/share/licenses/${pkgname}/license.html"
  install -d "${pkgdir}/opt/${pkgname}/platforms"
  install -d "${pkgdir}/opt/${pkgname}/add-ons"

  cp -a tools "${pkgdir}/opt/${pkgname}"

  # Fix broken permissions
  chmod -R o=g "${pkgdir}/opt/${pkgname}"
  find "${pkgdir}/opt/${pkgname}" -perm 744 -exec chmod 755 {} +
}
